package com.hkyp.ss.course.apis.dto.query;

import com.hkyp.ss.course.apis.dto.request.HomePageCourseDTO;
import lombok.Data;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Data
public class CourseSpecification implements Specification {
    // 是否是后台请求
    private boolean backOrnot = false;

    private HomePageCourseDTO homePageCourseDTO;

    public CourseSpecification(HomePageCourseDTO homePageCourseDTO){

        this.homePageCourseDTO = homePageCourseDTO;
    }

    @Override
    public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {


        List<Predicate> predicates = new ArrayList<Predicate>();

        if(homePageCourseDTO.getOfficialPrice() != null){
            predicates.add(criteriaBuilder.equal(root.<Integer>get("officialPrice"),homePageCourseDTO.getOfficialPrice()));

        }

        // 标题或课程来源
        if(!StringUtils.isEmpty(homePageCourseDTO.getPlatformOrTitle())){
            //  criteriaBuilder.ge() 是大于的意思
            Expression concat = criteriaBuilder.concat(root.<String>get("title"), root.<String>get("sourcePlatform"));
            predicates.add(criteriaBuilder.like(concat,"%"+homePageCourseDTO.getPlatformOrTitle()+"%"));

        }else {
            // 标题
            if(StringUtils.hasLength(homePageCourseDTO.getTitle())){
                //  criteriaBuilder.ge() 是大于的意思
                predicates.add(criteriaBuilder.like(root.<String>get("title"),"%"+homePageCourseDTO.getTitle()+"%"));
            }

            //课程来源
            if(!StringUtils.isEmpty(homePageCourseDTO.getSourcePlatform())){
                predicates.add(criteriaBuilder.like(root.<String>get("sourcePlatform"),"%"+homePageCourseDTO.getSourcePlatform()+"%"));
            }
        }
        if(homePageCourseDTO.getSourcePlatformCode() != null){
            predicates.add(criteriaBuilder.equal(root.<Integer>get("sourcePlatformCode"),homePageCourseDTO.getSourcePlatformCode()));
        }

        // 类型
        if(homePageCourseDTO.getCategory()!=null){

            long category = homePageCourseDTO.getCategory();

            long temp = 0;
            if(category % 10000 == 0){
                temp = 10000;
            }else if(category%100==0 && category%10000!=0){
                temp = 100;
            }else if(category%100!=0 ){
                temp = 0;
            }

            predicates.add(criteriaBuilder.between(root.<Integer>get("category"),category,category+temp));

        }

        // 大于该课评数的课程
        if(homePageCourseDTO.getCommentNum()!=null){
            predicates.add(criteriaBuilder.greaterThan(root.<Integer>get("commentNum"),homePageCourseDTO.getCommentNum()));
        }

        // 大于该评分的课程
        if(homePageCourseDTO.getComScore()!= null){
            predicates.add(criteriaBuilder.greaterThan(root.<Integer>get("comScore"),homePageCourseDTO.getComScore()));
        }


//				if(StringUtils.hasLength(person.getCreateAt()+"")){  where id = id and name = name;
//					//  criteriaBuilder.ge() 是大于等于的意思
//					predicates.add(criteriaBuilder.ge(root.<Long>get("createAt"),person.getCreateAt()));
//				}

        if(!backOrnot){
            predicates.add(criteriaBuilder.equal(root.<Integer>get("status"),0));
        }
        //未删除的
        predicates.add(criteriaBuilder.equal(root.<Integer>get("deleted"),0));

        return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}
